﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;

namespace DesignLayOut_QLQTPM_Team7.DAO
{
    class DataProvider
    {
        protected string m_connectionString;
        protected OleDbConnection connection;
        protected OleDbDataAdapter da = null;
        protected OleDbCommand command;

        //Phuong thuc
        public DataProvider()
        {
            m_connectionString = "Provider=Microsoft.ACE.OLEDB.12.0 ;Data Source=CSDLv1.3.accdb;Persist Security Info=False";
        }

        //Ket noi
        public void connect()
        {
            try
            {
                connection = new OleDbConnection(ConnectionString);
                connection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể kết nối được với Server\n" + ex.Message, "Lỗi chương trình");
                Application.Exit();
            }
        }

        //Tat ket noi
        public void disconnect()
        {
            try
            {
                connection.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        //Lay ra du lieu ra table
        public DataTable executeQuery(String sqlString)
        {
            connect();
            DataSet ds = new DataSet();
            da = new OleDbDataAdapter(sqlString, connection);
            da.Fill(ds);
            disconnect();
            return ds.Tables[0];
        }

        //Dung cho cac thao tac insert, delete, update
        public bool executeNonQuery(string sqlString)
        {

            //connect();
            //command = new OleDbCommand(sqlString, connection);
            //command.ExecuteNonQuery();
            //disconnect();

            connect();
            try
            {
                command = new OleDbCommand(sqlString, connection);
                command.ExecuteNonQuery();
            }
            catch (Exception)
            {
                disconnect();
                return false;
            }
            disconnect();
            return true;

        }

        //Lay 1 gia tri du lieu ra
        public object executeScalar(string sqlString)
        {
            connect();
            command = new OleDbCommand(sqlString, connection);
            object o = command.ExecuteScalar();
            disconnect();
            return o;
        }

        public static OleDbConnection ConnectionData()
        {
            string cnStr = "Provider=Microsoft.ACE.OLEDB.12.0 ;Data Source=CSDLv1.3.accdb;Persist Security Info=False";
            OleDbConnection cn = new OleDbConnection(cnStr);
            cn.Open();
            return cn;
        }

        public string ConnectionString
        {
            get
            {
                return m_connectionString;
            }
            set
            {
                m_connectionString = value;
            }
        }
    }
}
